/**
 * Created by xiezhouyan on 15-1-16.
 */
var express = require('express'),
    cookieParser = require('cookie-parser'),
    session = require('express-session'),
    bodyParser = require('body-parser'),
    path = require("path"),
    routes = require("./routes"),
    config = require("./config"),
    app = express();

// Middleware
app.use(cookieParser());
app.use(session({secret: 'lockdown-search', resave: false, saveUninitialized: false}));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));

// View
app.set('views', path.resolve(__dirname, "./views"));
app.set('view engine', 'ejs');
app.use(function (req, res, next) {
    res.locals.req = req;
    res.locals.res = res;
    next();
});
app.get('/', function (req, res) {
    res.render("index");
});

app.get("/search", routes.search);

var start = module.exports.start = function () {
    app.listen(config.server.port, function (err) {
        if (err) console.error(err);
        else console.log('Server started at ' + config.server.port);
    });
};
module.exports = app;

if (require.main == module) {
    start();
}